package com.example.demoserver.controller;

import com.example.demoserver.entity.InteGoods;
import com.example.demoserver.entity.InteLogistics;
import com.example.demoserver.entity.InteSaleorder;
import com.example.demoserver.service.InteLogisticsService;
import com.example.demoserver.service.InteSaleorderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/logistics")
@RequiredArgsConstructor
@Component
public class InteLogisticsController {
    private final InteLogisticsService inteLogisticsService;
    private final InteSaleorderService inteSaleorderService;
    @GetMapping("/selectall")
    public List<InteLogistics> selectAll(){
        List<InteLogistics> list=inteLogisticsService.selectAll();
        return list;
    }

    @PostMapping("/insert")
    public InteLogistics goodsInsert(@RequestBody InteLogistics inteLogistics){
        inteLogistics.setOrdertime(Util.getNowTime());
        inteLogistics.setLogisticstate(0);
        boolean bol=inteLogisticsService.save(inteLogistics);
        if(bol) return inteLogistics;
        return null;
    }

    @PostMapping("/update")
    public InteLogistics goodsUpdate(@RequestBody InteLogistics inteLogistics){
        boolean bol=inteLogisticsService.updateById(inteLogistics);
        if(bol) return inteLogistics;
        return null;
    }

    @GetMapping("/delete")
    public Integer goodsDelete(Integer id){
        return inteLogisticsService.deleteById(id);
    }

    @GetMapping("/dodelivery")
    public InteLogistics doDelivery(Integer id,Integer saleId){
        String time=Util.getNowTime();
        InteLogistics inteLogistics=new InteLogistics();
        inteLogistics.setId(id);
        inteLogistics.setDeliverytime(time);
        inteLogistics.setLogisticstate(1);
        inteLogisticsService.updateById(inteLogistics);
        InteSaleorder inteSaleorder=new InteSaleorder();
        inteSaleorder.setId(saleId);
        inteSaleorder.setDeliverytime(time);
        inteSaleorder.setOrderstate(4);
        inteSaleorderService.updateById(inteSaleorder);
        return inteLogistics;
    }

    @GetMapping("/overalllogistics")
    public List<Map<Object,Object>> overallLogistics(){
        List<InteLogistics> list1=inteLogisticsService.selectSome(0,null);
        List<InteLogistics> list2=inteLogisticsService.selectSome(1,null);
        List<Map<Object,Object>> list=new ArrayList<>();
        list.add(setMap(list1.size(),"待发货"));
        list.add(setMap(list2.size(),"已发货"));
        return list;
    }
    @GetMapping("/todaylogistics")
    public List<Map<Object,Object>> todayLogistics(){
        List<InteLogistics> list1=inteLogisticsService.selectSome(0,1);
        List<InteLogistics> list2=inteLogisticsService.selectSome(1,1);
        List<Map<Object,Object>> list=new ArrayList<>();
        list.add(setMap(list1.size(),"待发货"));
        list.add(setMap(list2.size(),"已发货"));
        return list;
    }
    @GetMapping("/sevendaylogistics")
    public Map<Object,Object> sevenDayProduction(){
        List<List<InteLogistics>> list=inteLogisticsService.sevenDayProduction();
        Map<Object,Object> map=new HashMap<>();
        List<Integer> value=new ArrayList<>();
        List<String> name=new ArrayList<>();
        for (List<InteLogistics> i:list) {
            value.add(i.size());
        }
        Collections.reverse(value);
        map.put("datax",getYearDay());
        map.put("datay",value);
        return map;
    }
    public List<String> getYearDay(){

        List<String> dates = new ArrayList<>();

        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 将过去七天的日期（包括今天）添加到列表中
        for (int i = 1; i < 8; i++) {
            dates.add(today.minusDays(i).toString());
        }

        // 对列表进行升序排序
        Collections.sort(dates);
        return dates;
    }
    public Map<Object,Object> setMap(Integer value,String name){
        Map<Object,Object> map=new HashMap<>();
        map.put("value",value);
        map.put("name",name);
        return  map;
    }
}
